/*
 * Db.java
 *
 * Created on September 15, 2006, 7:22 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.org.kasir.db;

/**
 *
 * @author City_Z
 */
import java.sql.*;
import java.util.ArrayList;


public class Db 
{
private Connection con = null;
private ResultSet rs = null;
private Statement s = null;

public void setAutoCommit(boolean status) throws SQLException{
    this.con.setAutoCommit(status);
}
public void commit() throws SQLException{
    this.con.commit();
}
public void roolBack() throws SQLException{
    this.con.rollback();
}


public Db(boolean pal)throws Exception
{
	String url = "jdbc:mysql://localhost/dbkasir";
	String user = "root";
	String pass = "admin";
	this.con = createConnection(url,user,pass);
	this.s = con.createStatement();
}
public int updateData(String sql) throws Exception 
{
	int x = 0;
	try
	{
		x = s.executeUpdate(sql);
	}
	catch(SQLException err)
	{
		System.out.println(err.getMessage());
	}
	return x;
}
public void closer()throws Exception
{
	if(this.con!=null)
	{
		try
		{
		this.con.close();
		}
		catch(SQLException e)
		{
			System.out.println(e.getMessage());
		}
	}
	if(this.s!=null)
	{
		try
		{
			this.s.close();
		}
		catch(SQLException e1)
		{
			System.out.println(e1.getMessage());
		}
	}
	
}
public ArrayList getData(String sql)throws Exception{
	ArrayList result = new ArrayList();		
	result.clear();
	try {
		rs = s.executeQuery(sql);
		ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount =  rsmd.getColumnCount();              
        int posisi=0;
        while(rs.next()) {
        	ArrayList temp = new ArrayList();
        	temp.clear();
        	for(int i=1;i<=columnCount;i++){
        	    if(rs.getString(i)==null){
        	        temp.add("0");            	       
        	    }else{
        	        temp.add(rs.getString(i));
        	    }
        	}            	
        	result.add(posisi,temp);
        	++posisi;
        
        }                      
	} catch (SQLException e) {
		throw new Exception(e.getMessage());
	}		
	return result;
}	
	
	

public Connection createConnection(String dburl,String dbuser,String dbpass)throws Exception
{
	Connection conn = null;
	try
	{
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		conn = DriverManager.getConnection(dburl,dbuser,dbpass);
	}
	catch(ClassNotFoundException er)
	{
		System.out.println(er.getMessage());
	}
	catch(SQLException er1)
	{
		System.out.println(er1.getMessage());
	}
	catch(IllegalAccessException er3)
	{
		System.out.println(er3.getMessage());
	}
	catch(InstantiationException er5)
	{
		System.out.println(er5.getMessage());
	}
	return conn;
	
	
}

}

